var express = require('express');
var menuModel = require('../model/menu')
var router = express.Router();

// 添加主菜单
router.post('/add/main',async (req, res)=>{
    const {title,path,component,name,show=1} = req.body;
    let resData = await menuModel.create({
        title,path,component,name,show,children:[]
    })
    res.send({
        msg:'添加成功',
        data:resData,
        code:1
      })
})
// 编辑主菜单
router.post('/update/main',async (req, res)=>{
    const {_id,title,path,component,name,show} = req.body;
    let resData = await menuModel.updateOne({
        _id:_id
    },{
        title,path,component,name,show
    })
    console.log(resData);
    
   if(resData.modifiedCount){
        res.send({
            msg:'编辑成功',
            code:1,
            data:resData
        })
   } else {
        res.send({
            msg:'编辑失败',
            code:0
        })
   }
    
})

// 添加子菜单
router.post('/add/sub',async (req,res)=>{
    const {parentId, title,path,component,name,show } = req.body;
    let resData = await menuModel.findById(parentId);
    let isHas =  resData.children.some((item)=>{
        return item.path == path
    })
    if(!isHas) {
        resData.children = [...resData.children, {title,path,component,name,_id:Date.now()+'',show}]
    }
   
    await menuModel.updateOne({_id:parentId},{
        children:resData.children
    })

    res.send({
        msg:'获取成功',
        code:1,
        data:resData
      })
})
// 编辑子菜单
router.post('/update/sub',async (req,res)=>{
    const {parentId, _id, title,path,component,name,show } = req.body;
    let resData = await menuModel.findById(parentId);
   
    resData.children.forEach((item)=>{
        if(item._id == _id) {
            item.title = title;
            item.path = path;
            item.component = component;
            item.name = name;
            item.show = show;
        }
    })
    await menuModel.updateOne({_id:parentId},{
        children:resData.children
    })

    res.send({
        msg:'编辑成功',
        code:1,
        data:resData
      })
})


// 删除主菜单
router.delete('/delete',async (req,res)=>{
   const {parentId} = req.query;
   const resData =  await menuModel.deleteOne({_id:parentId})
   console.log(resData);
   
    res.send({
        msg:'删除成功',
        code:1
      })

})

// 删除子菜单
router.delete('/delete/sub',async (req,res)=>{
    const {id,parentId} = req.query;
    const resData =  await menuModel.findById(parentId);
    const datas =  resData.children.filter((item)=>{
        return item._id != id
    })

   const result = await menuModel.updateOne({
        _id:parentId
    },{
        children:datas
    })
    if(result.modifiedCount){
        res.send({
            msg:'删除成功',
            code:1,
            data:datas
          })   
    } else {
        res.send({
            msg:'删除失败',
            code:0
          }) 
    }
 })

// 获取菜单列表
router.get('/list',async function(req, res, next) {
    
  let resData =   await menuModel.find()

  res.send({
    msg:'获取成功',
    data:resData,
    code:1
  })
});

module.exports = router;
